home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 52 / Amiga Format AFCD52 (Issue 136, May 2000).iso / -serious- / programming / other / systracker_src / src / st_memory.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-02-28  |  3.3 KB  |  130 lines

  1. /***************************************************************************/
  2. /* st_memory.c - Memory control module.                                    */
  3. /*                                                                         */
  4. /* Copyright © 1999-2000 Andrew Bell. All rights reserved.                 */
  5. /***************************************************************************/
  6.  
  7. #include "SysTracker_rev.h"
  8. #include "st_include.h"
  9. #include "st_protos.h"
  10. #include "st_strings.h"
  11.  
  12. /***************************************************************************/
  13. /* Data and defines */
  14.  
  15. APTR MemPool = NULL;
  16. struct SignalSemaphore MemPoolKey;
  17.  
  18. /***************************************************************************/
  19.  
  20. GPROTO BOOL MEM_Init( void )
  21. {
  22.   /*********************************************************************
  23.    *
  24.    * MEM_Init()
  25.    *
  26.    * Initialize the memory resources. This includes creating the main
  27.    * memory pool and initializing a semaphore for it.
  28.    *
  29.    *********************************************************************
  30.    *
  31.    */
  32.   
  33.   if (!(MemPool = (APTR) CreatePool(MEMF_CLEAR | MEMF_PUBLIC,
  34.                             POOL_PUDDLESIZE, POOL_THRESHSIZE)))
  35.     return FALSE;
  36.  
  37.   memset(&MemPoolKey, 0, sizeof(struct SignalSemaphore));
  38.   InitSemaphore((struct SignalSemaphore *) &MemPoolKey);
  39.   return TRUE;
  40. }
  41.  
  42. GPROTO void MEM_Free( void )
  43. {
  44.   /*********************************************************************
  45.    *
  46.    * MEM_Free()
  47.    * 
  48.    * Release the resources allocated by MEM_Init().
  49.    *
  50.    *********************************************************************
  51.    *
  52.    */
  53.   
  54.   if (MemPool)
  55.   {
  56.     DeletePool(MemPool); MemPool = NULL;
  57.   }
  58. }
  59.  
  60. GPROTO APTR MEM_AllocVec( ULONG Size )
  61. {
  62.   /*********************************************************************
  63.    *
  64.    * MEM_AllocVec()
  65.    *
  66.    * Allocate a vector using the main memory pool. Semaphore protected.
  67.    *
  68.    *********************************************************************
  69.    *
  70.    */
  71.   
  72.   register ULONG *Vec;
  73.   Size += 4;
  74.   ObtainSemaphore((struct SignalSemaphore *) &MemPoolKey);  
  75.   if (Vec = AllocPooled(MemPool, Size))
  76.     *Vec++ = Size;
  77.   ReleaseSemaphore((struct SignalSemaphore *) &MemPoolKey);
  78.  
  79.   return (APTR) Vec;
  80. }
  81.  
  82. GPROTO void MEM_FreeVec( APTR Vec )
  83. {
  84.   /*********************************************************************
  85.    *
  86.    * MEM_FreeVec()
  87.    *
  88.    * Free a vector that was allocated with the MEM_AllocVec() function.
  89.    * Semaphore protected.
  90.    *
  91.    *********************************************************************
  92.    *
  93.    */
  94.  
  95.   if (Vec)
  96.   { 
  97.     ObtainSemaphore((struct SignalSemaphore *) &MemPoolKey);
  98.     FreePooled(MemPool, ((UBYTE *) Vec) - 4, ((ULONG *) Vec)[-1]);
  99.     ReleaseSemaphore((struct SignalSemaphore *) &MemPoolKey);
  100.   }
  101. }
  102.  
  103. GPROTO UBYTE *MEM_StrToVec( UBYTE *Str )
  104. {
  105.   /*********************************************************************
  106.    *
  107.    * MEM_StrToVec()
  108.    *
  109.    * Copy a NULL terminated string to a vector, this routine uses
  110.    * MEM_AllocVec() for the actual allocation of the vector, so the
  111.    * result of this call should eventually be freed by MEM_FreeVec().
  112.    *
  113.    * (This should really be in st_strings.c!)
  114.    *
  115.    *********************************************************************
  116.    *
  117.    */
  118.  
  119.   register UBYTE *StrBuf;
  120.  
  121.   if (!Str) return NULL;
  122.   if (StrBuf = MEM_AllocVec(strlen(Str) + 1))
  123.     strcpy(StrBuf, Str);
  124.  
  125.   return StrBuf;
  126. }
  127.  
  128.  
  129.  
  130.